package com.iflytek.voicecloud.rtasr.test;


import java.util.Arrays;
import java.util.List;

public class Service {
    public static void main(String[] args) {
        // 构造测试数据
        List<SentenceInfo> sentences = Arrays.asList(
                new SentenceInfo(
                        "然后错误等级是11，这样嗯我们数据库对这个错误等级要求不高，",
                        0, 6250, 16),
                new SentenceInfo(
                        "我记得有一个语言啊它是要它是每个错误等级出来了这个。",
                        6250, 10120, 17)
        );

        // 调用方法并输出结果
        String result = buildEnhancedResult(sentences);
        System.out.println(result);
    }

    private static String buildEnhancedResult(List<SentenceInfo> sentences) {
        StringBuilder finalResult = new StringBuilder();
        long prevEd = 0; // 上一句的结束时间

        for (SentenceInfo sentence : sentences) {
            // 打印每个 SentenceInfo 的详细信息
            System.out.println("SentenceInfo: text=" + sentence.text +
                    ", bg=" + sentence.bg +
                    ", ed=" + sentence.ed +
                    ", wordCount=" + sentence.wordCount);

            // 计算与上一句之间的停顿时间（单位：ms）
            long pauseTime = prevEd > 0 ? sentence.ed - prevEd : 0;

            // 计算语速（字/秒）
            double durationSec = (sentence.ed - sentence.bg) / 1000.0;
            String speedLevel = "快"; // 默认是“快”
            double speed = 0;

            if (durationSec > 0) {
                speed = sentence.wordCount / durationSec;
                if (speed < 3) {
                    speedLevel = "慢";
                } else if (speed < 5) {
                    speedLevel = "中等";
                }
            }

            if (pauseTime > 0) {
                finalResult.append(String.format("(停顿%d秒)", pauseTime / 1000));
            }

            // 拼接句子 + 停顿时长 + 语速分析（含具体数值）
            finalResult.append(sentence.text);

            // 添加语速级别和具体数值，保留一位小数
            finalResult.append(String.format("(语速%s，%.1f字/秒）", speedLevel, speed));

            prevEd = sentence.ed; // 更新上一句结束时间
        }

        return finalResult.toString();
    }

    private static class SentenceInfo {
        String text;
        long bg;
        long ed;
        int wordCount;

        public SentenceInfo(String text, long bg, long ed, int wordCount) {
            this.text = text;
            this.bg = bg;
            this.ed = ed;
            this.wordCount = wordCount;
        }
    }
}